package com.itcx.ui;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener,ActionListener {

    int[][] data = new int[4][4];

    public GameJFrame() {


        randomphoto();


        initialJFrame();


        initialJMenu();


        initialImage();


        this.setVisible(true);


    }

    //设定步数
    int step =0;

    //放置图片
    private void initialImage() {
        //先移除图片
        this.getContentPane().removeAll();

        if(victory()){
            ImageIcon victory = new ImageIcon("D:\\Java\\data\\jigsawgame\\cube2\\胜利.png");
            JLabel vicLabel = new JLabel(victory);
            vicLabel.setBounds(203,283,197,73);
            this.getContentPane().add(vicLabel);
        }

        JLabel stepcount = new JLabel("步数"+step);
        stepcount.setBounds(50,30,100,20);
        this.getContentPane().add(stepcount);

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int num = data[i][j];
                ImageIcon photo = new ImageIcon("D:\\Java\\data\\jigsawgame\\cube2\\" + num + ".jpg");
                JLabel imageLabel = new JLabel(photo);
                imageLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
                imageLabel.setBorder(new BevelBorder(1));
                this.getContentPane().add(imageLabel);
            }
        }
        //添加背景
        ImageIcon background = new ImageIcon("D:\\Java\\data\\jigsawgame\\cube2\\背景.png");
        JLabel backgroundLabel = new JLabel(background);
        backgroundLabel.setBounds(36, 45, 508, 560);
        this.getContentPane().add(backgroundLabel);
        //刷新界面
        this.getContentPane().repaint();


    }

    //确定0的位置
    int x;
    int y;

    int [][] win ={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

    //随机的二维列表
    private void randomphoto() {

        int[] list = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

        Random rand = new Random();

        for (int i = 0; i < list.length; i++) {
            int index = rand.nextInt(list.length);
            int change = list[i];
            list[i] = list[index];
            list[index] = change;
        }

        for (int i = 0; i < list.length; i++) {
            if (list[i] == 0) {
                x = i / 4;
                y = i % 4;
            }
            data[i / 4][i % 4] = list[i];
        }
    }

    //创建菜单
    private void initialJMenu() {
        //创建整个菜单
        JMenuBar jMenuBar = new JMenuBar();
        //菜单
        JMenu jfunction = new JMenu("功能");
        JMenu jabout = new JMenu("关于我们");

        jMenuBar.add(jfunction);
        jMenuBar.add(jabout);

        //菜单下条目
        JMenuItem regame = new JMenuItem("重新游戏");
        JMenuItem quit = new JMenuItem("关闭游戏");

        regame.addActionListener(this);
        quit.addActionListener(this);


        jfunction.add(regame);
        jfunction.add(quit);

        this.setJMenuBar(jMenuBar);
    }

    //创建框架
    private void initialJFrame() {
        this.setSize(603, 680);
        this.setAlwaysOnTop(true);
        //居中
        this.setLocationRelativeTo(null);
        //关闭同时关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        //键盘监听
        this.addKeyListener(this);

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if (code == KeyEvent.VK_A) {
            this.getContentPane().removeAll();
            ImageIcon all = new ImageIcon("D:\\Java\\data\\jigsawgame\\cube2\\all.jpg");
            JLabel allLabel = new JLabel(all);
            allLabel.setBounds(83, 134, 420, 420);
            this.getContentPane().add(allLabel);

            //重新添加背景
            ImageIcon background = new ImageIcon("D:\\Java\\data\\jigsawgame\\cube2\\背景.png");
            JLabel backgroundLabel = new JLabel(background);
            backgroundLabel.setBounds(36, 45, 508, 560);
            this.getContentPane().add(backgroundLabel);
            //刷新界面
            this.getContentPane().repaint();
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {

        if (victory()){
            return;
        }

        int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT) {
            if (y != 3) {
                data[x][y] = data[x][y + 1];
                data[x][y + 1] = 0;
                y++;
                step++;
                initialImage();
            }
        } else if (key == KeyEvent.VK_RIGHT) {
            if (y != 0) {
                data[x][y] = data[x][y - 1];
                data[x][y - 1] = 0;
                y--;
                step++;
                initialImage();
            }
        } else if (key == KeyEvent.VK_UP) {
            if (x != 3) {
                data[x][y] = data[x + 1][y];
                data[x + 1][y] = 0;
                x++;
                step++;
                initialImage();
            }
        } else if (key == KeyEvent.VK_DOWN) {
            if (x != 0) {
                data[x][y] = data[x - 1][y];
                data[x - 1][y] = 0;
                x--;
                step++;
                initialImage();
            }
        } else if (key == KeyEvent.VK_A) {
            initialImage();
        } else if (key == KeyEvent.VK_O) {
            data = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
            initialImage();
        }

    }

    private Boolean victory(){
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j]!=win[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("重新游戏")) {
            randomphoto();
            initialJFrame();
            initialJMenu();
            initialImage();
            this.setVisible(true);
        }
        if (e.getActionCommand().equals("关闭游戏")) {
            System.exit(0);
        }

    }
}